package com.liam.apppush.client.xiaomi;

import com.alibaba.fastjson.JSONObject;
import com.liam.apppush.config.XiaoMiConfig;
import com.liam.apppush.vo.MessageVo;
import com.xiaomi.xmpush.server.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 小米推送
 *  1 批量发送  单次不超过1000
 *  2 订阅/取消发送    单次不超过1000
 *  3 全量
 */
@Slf4j
@Component
public class XiaoMiClient extends XiaoMiConfig {

    private Sender sender = null;

    public Sender getSender(){
        if(sender == null){
            synchronized (this){
                if(sender == null){
                    sender = new Sender(appSecret);
                }
            }
        }
        return sender;
    }

    /**
     * 发送单聊
     * @param messageVo
     * @throws Exception
     */
    public void sendMessage(MessageVo messageVo) throws Exception {
        Constants.useOfficial();
        String messagePayload = "This is a message";
        String title = "notification title";
        String description = "notification description";
        Message message = new Message.Builder()
                .title(title)
                .description(description).payload(messagePayload)
                .restrictedPackageName(packageName)
                .notifyType(1)
                .build();
        Result result = getSender().send(message, messageVo.getCId(), 3);
//        Result result = sender.send(message, messageVo.getCIds(), 3);
        log.info(JSONObject.toJSONString(result));
    }

    /**
     * 发送topic消息时topic的数量不能超过5；
     *
     * @param messageVo
     * @throws Exception
     */
    public void sendTopicMessage(MessageVo messageVo) throws Exception {
        Constants.useOfficial();
        String messagePayload = "This is a message";
        String title = "notification title";
        String description = "notification description";
        List<String> topicList = new ArrayList<>();
        String topic1 = "testTopic1";
        String topic2 = "testTopic2";
        topicList.add(topic1);
        topicList.add(topic2);
        Message message = new Message.Builder()
                .title(title)
                .description(description).payload(messagePayload)
                .restrictedPackageName(packageName)
                .notifyType(1)     // 使用默认提示音提示

                // 打开指定app页面
//                .extra(Constants.EXTRA_PARAM_NOTIFY_EFFECT, Constants.NOTIFY_ACTIVITY)
//                .extra(Constants.EXTRA_PARAM_INTENT_URI, "intent:#Intent;component=com.xiaomi.mipushdemo/.NewsActivity;end")

                // 打开指定网页
//                .extra(Constants.EXTRA_PARAM_NOTIFY_EFFECT, Constants.NOTIFY_WEB)
//                .extra(Constants.EXTRA_PARAM_WEB_URI, "http://www.xiaomi.com")

                .build();
        //根据topicList做并集运算, 发送消息到指定一组设备上
        Result result = getSender().multiTopicBroadcast(message, topicList, Sender.BROADCAST_TOPIC_OP.UNION, 3);
//        sender.broadcastAll(message, 3);  全量发送

        log.info(JSONObject.toJSONString(result));
    }

    /**
     * 发送全量
     * @param messageVo
     * @throws Exception
     */
    public void sendAllMessage(MessageVo messageVo) throws Exception {
        Constants.useOfficial();
        String messagePayload = "This is a message";
        String title = "notification title";
        String description = "notification description";
        List<String> topicList = new ArrayList<>();
        String topic1 = "testTopic1";
        String topic2 = "testTopic2";
        topicList.add(topic1);
        topicList.add(topic2);
        Message message = new Message.Builder()
                .title(title)
                .description(description).payload(messagePayload)
                .restrictedPackageName(packageName)
                .notifyType(1)     // 使用默认提示音提示
                .build();
        //根据topicList做并集运算, 发送消息到指定一组设备上
        Result result =  getSender().broadcastAll(message, 3);
        log.info(JSONObject.toJSONString(result));
    }


    /**
     * 设置分组
     * @param messageVo
     * @throws Exception
     */
    public void subscribeTopic(MessageVo messageVo) throws Exception {
        Subscription subscription = new Subscription(appSecret);
        Result result =  subscription.subscribeTopic(messageVo.getCIds(),"分组名称",null,3);
        log.info(JSONObject.toJSONString(result));
    }

    /**
     * 解除分组
     * @param messageVo
     * @throws Exception
     */
    public void unSubscribeTopic(MessageVo messageVo) throws Exception {
        Subscription subscription = new Subscription(appSecret);
        Result result =  subscription.unsubscribeTopic(messageVo.getCIds(),"分组名称",null,3);
        log.info(JSONObject.toJSONString(result));
    }



}
